//
//  FSManagerPageController.m
//  TextPage
//
//  Created by 刘沿 on 16/6/30.
//  Copyright © 2016年 刘沿. All rights reserved.
//
#import "huiyishiTableViewController.h"
#import "shitangTableViewController.h"

#import "FSManagerPageController.h"
#import "SelectAlert.h"

#import "FSMenuView.h"

@interface FSManagerPageController ()<UIScrollViewDelegate>{
    NSInteger selectIndex;
        NSArray *titles;

}


@property (nonatomic,strong) UIScrollView *pageScrollView;

@property (nonatomic,strong) FSMenuView *menuView;

@end

@implementation FSManagerPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone; 
    self.pageScrollView = [[UIScrollView alloc] init];

    self.pageScrollView.delegate = self;
    /*
     （1）当bounces属性设置为YES时，当UIScrollView中图片滑动到边界的时候会出现弹动的效果，就像是Linux中的果冻效果一样。
     
     （2）当bounces属性设置为NO时，当UIScrollView中图片滑动到边界时会直接定在边界就不会有弹动的效果。
     
     */
    self.pageScrollView.bounces = NO;
    
    // 控制控件是否整页翻动
    self.pageScrollView.pagingEnabled = YES;
    
    //竖直方向不滑动
    self.pageScrollView.showsHorizontalScrollIndicator = NO;
    
    [self.view addSubview:self.pageScrollView];
    
//菜单
    FSMenuView *menu = [[FSMenuView alloc] init];
    
    self.menuView = menu;
    
    [self.view addSubview:menu];
    
//关联动画
    menu.scollView = self.pageScrollView;
    
     UIBarButtonItem * rightIem = [[UIBarButtonItem alloc] initWithTitle:@"新建申请单" style:UIBarButtonItemStylePlain target:self action:@selector(Actiondo)];
        self.navigationItem.rightBarButtonItem = rightIem;
 
    
    
}
// 选择表单类型
- (void)Actiondo{
    titles = @[@"会议申请表单",@"食堂申请表单"];
[SelectAlert showWithTitle:@"表单类型" titles:titles selectIndex:^(NSInteger selectIndex) {
    } selectValue:^(NSString *selectValue) {
    NSLog(@"%@",selectValue);
if ([selectValue isEqualToString:@"会议申请表单"]) {
//[[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"sex"];
//[self viewWillAppear:YES];
huiyishiTableViewController *vc = [[huiyishiTableViewController alloc]init];
[self.navigationController pushViewController: vc animated:YES];
}else{
//
shitangTableViewController *vc = [[shitangTableViewController alloc]init];
[self.navigationController pushViewController: vc animated:YES];}


} showCloseButton:YES];
}

- (void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];

    CGFloat w = self.view.frame.size.width;
    //设置宽度
    self.menuView.width = w;
    self.menuView.frame = CGRectMake(0, 0, w, 40);

    CGFloat y = CGRectGetMaxY(self.menuView.frame);
    
    CGFloat h = self.view.frame.size.height - y;
    
    self.pageScrollView.frame = CGRectMake(0, y, w, h);
    self.pageScrollView.contentSize = CGSizeMake(w * 2, h);
    //计算控制器的位置

    self.firstVC.view.frame = CGRectMake(w, 0, w, h);
    
    self.secVC.view.frame = CGRectMake(0, 0, w, h);

}

//重写set方法设置页面
- (void)setFirstVC:(FSFirstViewController *)firstVC{

    if (firstVC) {
        _firstVC = firstVC;
        
        if (self.pageScrollView) {
            
            [self addChildViewController:firstVC];
            [self.pageScrollView addSubview:firstVC.view];
        }
        
    }

}

-(void)setSecVC:(FSSecViewController *)secVC{

    if (secVC) {
        _secVC = secVC;
        
        if (self.pageScrollView) {
            [self addChildViewController:secVC];
            
            [self.pageScrollView addSubview:secVC.view];
            
        }
        
    }
    

}

//开始
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat offsetX = scrollView.contentOffset.x;

    CGFloat progress = offsetX / scrollView.contentSize.width;

    [self.menuView animateViewProgress:progress];

}


//开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    
    /*
     如果父视图为ParentView包含一个Button，如果再ParentView上添加子视图ChildView，且ChildView盖住了Button，那么Button就得到不响应了，为了让Button响应，可以设置ChildView的userInteractionEnabled ＝ NO
     */
    self.menuView.userInteractionEnabled = NO;
}

//结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    self.menuView.userInteractionEnabled = YES;
}




@end




























